﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using log4net;

namespace Koonsoft.Tool.CheckEveryday.Order.DAL
{
    public class TodayOrder
    {
        private readonly static ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private SqlConnection sqlconn;
        private SqlCommand sqlcmd;
        private SqlDataReader sqlrd;

        /// <summary>
        /// 获取当日订单信息
        /// </summary>
        /// <returns></returns>
        public TodayOrderInfo GetTodayOrder()
        {
            TodayOrderInfo todayOrder = null;

            sqlconn = new SqlConnection();
            sqlconn.ConnectionString = ConfigurationManager.ConnectionStrings["ECSSCoreDB"].ConnectionString;

            sqlcmd = new SqlCommand();
            sqlcmd.Connection = sqlconn;
            sqlcmd.CommandText = "poll_statistictodayorders";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            
            try
            {
                sqlcmd.Connection.Open();
                sqlrd = sqlcmd.ExecuteReader();

                if (sqlrd.HasRows)
                {
                    sqlrd.Read();

                    todayOrder = new TodayOrderInfo
                    {
                        TotalOrders = (int)sqlrd["todaytotalorders"],
                        FinishedOrders = (int)sqlrd["todayfinishedorders"],
                        ExceptionOrders = (int)sqlrd["todayexceptionorders"]
                    };                    
                }
            }
            catch (Exception ex)
            {
                log.Error("统计当日订单出错", ex);
            }
            finally
            {
                sqlcmd.Connection.Close();
            }       

            return todayOrder;
        }
    }
}
